import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

import com.chen.model.Eclass;


public class jetty_run {

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		Server server = new Server();
		Connector connector = new org.mortbay.jetty.nio.SelectChannelConnector();

		connector.setHost(args != null && args.length > 0 ? args[0] : "0.0.0.0");
		connector.setPort(args != null && args.length > 1 ? Integer.parseInt(args[1]) : 8088);
		connector.setMaxIdleTime(30000);
		connector.setStatsOn(false);
		connector.setLowResourceMaxIdleTime(5000);

		// 定义上下文
		WebAppContext webapp = new WebAppContext();
		webapp.setContextPath("/accumulation");
		webapp.setWar(args != null && args.length > 2 ? args[2] : "./src/main/webapp");

		// 这个是默认的web.xml配置。下面两行可以注释掉
		webapp.setDefaultsDescriptor("./src/test/resources/webdefault.xml");
		server.setStopAtShutdown(true);
		server.setHandler(webapp);

		// InputStream resourceAsStream =
		// JettyLauncher.class.getClassLoader().getResourceAsStream("jetty-config.xml");
		// System.out.println(resourceAsStream);
		// XmlConfiguration configuration = new
		// XmlConfiguration(resourceAsStream);
		// server.setDefaultsDescriptor("src/test/resources/webdefault.xml");
		// configuration.configure(server);
		server.addConnector(connector);
		server.start();
		System.out.println("Jetty started in " + (System.currentTimeMillis() - start) + " ms.");
		
		//testHibernate();
	}

	public static void testHibernate() {
		Configuration config = new Configuration();
		config.configure();// 配置文件名不是hibernate.cfg.xml时将文件名传入
		Session session = config.buildSessionFactory().getCurrentSession();;// 相当于JDBC的注册驱动
	//	Session session = sessionFactory.openSession();// 相当于JDBC的getConnection
		Transaction tx = session.beginTransaction();// Hibernate操作必须启动事务
		Eclass ec = new Eclass();// 创建持久化对象
		ec.setClassID("Z");
		session.save(ec);// 保存对象
		tx.commit();// 持久化到数据库中
	}
}
